import datetime
import os
from Extend.FileMethod import FileExtend
from LogFIle.ImosLog import LogFile, LogFileControl


class LogCollation:

    @staticmethod
    def collationLog(server_stat: bool, collation_date=None, subtraction=None):
        if collation_date:
            file_date = collation_date
        elif subtraction is not None:
            if int(subtraction) == 0 and server_stat:
                raise SystemError("本地日志服务在运行时无法整理当天日志！")
            elif int(subtraction) == 0:
                file_date = datetime.datetime.now().strftime('%F')
            else:
                file_date = (datetime.datetime.now() - datetime.timedelta(days=int(subtraction))).strftime('%F')
        else:
            raise ValueError('除了路径之外必须传入一个时间')
        LogCollation.collation(file_date)
        print("\n\n整理成功!")

    @staticmethod
    def collation(file_date):
        content = LogFile.getLog(file_date)
        LogFile.dateSort(content)
        real_content = LogFile.listDictToContent(content)
        file_name = os.path.join(LogFileControl.path, file_date + ".log")
        FileExtend.writeFile(file_name, real_content)
